<?php

namespace App\Http\Controllers\VipChannel\Charge;

use App\Http\Controllers\VipChannel\BaseController;
use App\Http\Controllers\VipChannel\Charge\Transformers\ChargeInfoTransformer;
use App\Modules\Channel\Models\Channel;
use App\Modules\Channel\Models\ChannelUser;
use App\Modules\SendOrder\Models\SendOrder;
use App\Modules\Util;
use DB;
use Illuminate\Http\Request;

class ChargeController extends BaseController
{
    public function getDataInfo(Request $request)
    {
        $date = date('Y-m-d', strtotime('-1 day'));
        $channels = $this->getDistributionChannelIds($request);
        if (count($channels) <= 0) {
            return response()->error('NOT_EXIST');
        } else {
            $result = Channel::getChannelChargeData(['distribution_channel_ids' => $channels], true);
            $last_month_end = date("Y-m-31", strtotime("-1 month"));
            $last_month_begin = date("Y-m-1", strtotime("-1 month"));
            $charge_amount_yesterday = DB::table('order_day_stats')->whereIn('distribution_channel_id', $channels)->where('date', $date)->sum('total_recharge_amount');
            $subscribe_amount_last_month = DB::table('order_day_stats')->whereIn('distribution_channel_id', $channels)->whereBetween('date', [$last_month_begin, $last_month_end])->sum('subscribe_amount');
            $subscribe_user_num_last_month = DB::table('order_day_stats')->whereIn('distribution_channel_id', $channels)->whereBetween('date', [$last_month_begin, $last_month_end])->sum('subscribe_user_num');

            foreach ($result as $item) {
                $item->charge_amount_yesterday = $charge_amount_yesterday;
                $item->created_at = $item->created_at->toDateTimeString();
                $item->start_day_count = ceil((time() - strtotime($item->created_at)) / 86400);
                $item->cost_total = SendOrder::getDuringCostByDistributionChannelIds($channels, $date . ' 23:59:59');
                $item->charge_cost_percent_total = Util:: getPersentAmount($item->charge_amount_total, $item->cost_total);
                $item->charge_cost_percent_yesterday = Util:: getPersentAmount($item->charge_amount_yesterday, $item->cost_total);
                $item->arup_new_fans_last_month = (0 == $subscribe_user_num_last_month) ? 0 : round((float)$subscribe_amount_last_month / $subscribe_user_num_last_month, 2);
            }
        }
        return response()->success($result);
    }

    public function getCatalogDataInfo(Request $request)
    {
        $type = $request->has('type') ? $request->input('type') : 'site';
        if (!in_array($type, ['site', 'account'])) {
            return response()->error('PARAM_ERROR');
        }

        $date = date('Y-m-d', strtotime('-1 day'));
        $channels = $this->getDistributionChannelIds($request);
        if (count($channels) <= 0) {
            return response()->error('NOT_EXIST');
        }
        $params = ['distribution_channel_ids' => $channels, 'groupBy' => $type];
        $result = Channel::getChannelChargeData($params, false);
        $last_month_end = date("Y-m-31", strtotime("-1 month"));
        $last_month_begin = date("Y-m-1", strtotime("-1 month"));

        foreach ($result as $item) {
            //按账号的
            if ($type == 'account') {
                $channel_user_id = $item->id;
                $item->phone = $item->phone . '(' . $item->channel_user_nickname . ')';
                $channels = ChannelUser::getChannelIdArray($channel_user_id);
                $item->cost_total = SendOrder::getDuringCostByDistributionChannelIds($channels, $date . ' 23:59:59');
                $item->charge_amount_yesterday = DB::table('order_day_stats')->whereIn('distribution_channel_id', $channels)->where('date', $date)->sum('total_recharge_amount');
                $subscribe_amount_last_month = DB::table('order_day_stats')->whereIn('distribution_channel_id', $channels)->whereBetween('date', [$last_month_begin, $last_month_end])->sum('subscribe_amount');
                $subscribe_user_num_last_month = DB::table('order_day_stats')->whereIn('distribution_channel_id', $channels)->whereBetween('date', [$last_month_begin, $last_month_end])->sum('subscribe_user_num');
                //按站点的
            } else {
                $distribution_channel_id = $item->distribution_channel_id;
                $name = $item->site_nick_name;
                $item->distribution_channel_id = $distribution_channel_id . '(' . $name . ')';
                $channel = Channel::getIdById($distribution_channel_id);
                $item->created_at = $channel->created_at;
                $item->charge_amount_yesterday = DB::table('order_day_stats')->where('distribution_channel_id', $distribution_channel_id)->where('date', $date)->sum('total_recharge_amount');
                $subscribe_amount_last_month = DB::table('order_day_stats')->where('distribution_channel_id', $distribution_channel_id)->whereBetween('date', [$last_month_begin, $last_month_end])->sum('subscribe_amount');
                $subscribe_user_num_last_month = DB::table('order_day_stats')->where('distribution_channel_id', $distribution_channel_id)->whereBetween('date', [$last_month_begin, $last_month_end])->sum('subscribe_user_num');
                $item->cost_total = SendOrder::getDuringCostByChannelId($distribution_channel_id, $date . ' 23:59:59');
            }
            $item->start_day_count = ceil((time() - strtotime($item->created_at)) / 86400);
            $item->arup_new_fans_last_month = (0 == $subscribe_user_num_last_month) ? 0 : round((float)$subscribe_amount_last_month / $subscribe_user_num_last_month, 2);
            $item->charge_cost_percent_total = Util:: getPersentAmount($item->charge_amount_total, $item->cost_total);
            $item->charge_cost_percent_yesterday = Util:: getPersentAmount($item->charge_amount_yesterday, $item->cost_total);
        }
        return response()->pagination(new ChargeInfoTransformer(), $result);
    }

    public function exportCatalogDataInfo(Request $request)
    {
        $type = $request->has('type') ? $request->input('type') : 'site';
        if (!in_array($type, ['site', 'account'])) {
            return response()->error('PARAM_ERROR');
        }

        $date = date('Y-m-d', strtotime('-1 day'));
        $channels = $this->getDistributionChannelIds($request);
        if (count($channels) <= 0) {
            return response()->error('NOT_EXIST');
        }
        $params = ['distribution_channel_ids' => $channels, 'groupBy' => $type];
        $result = Channel::getChannelChargeData($params, true);
        $last_month_end = date("Y-m-31", strtotime("-1 month"));
        $last_month_begin = date("Y-m-1", strtotime("-1 month"));
        $header = [($type == 'account') ? '账号' : '站点', '名称', '充值总额', '成本总额', '整体回本率', '昨日充值', '昨日回本率', '投放开始时间', '运营天数', '上月新关arpu'];

        $data = [];
        foreach ($result as $item) {
            //按账号的
            if ($type == 'account') {
                $channel_user_id = $item->id;
                $channels = ChannelUser::getChannelIdArray($channel_user_id);
                $item->cost_total = SendOrder::getDuringCostByDistributionChannelIds($channels, $date . ' 23:59:59');
                $item->charge_amount_yesterday = DB::table('order_day_stats')->whereIn('distribution_channel_id', $channels)->where('date', $date)->sum('total_recharge_amount');
                $subscribe_amount_last_month = DB::table('order_day_stats')->whereIn('distribution_channel_id', $channels)->whereBetween('date', [$last_month_begin, $last_month_end])->sum('subscribe_amount');
                $subscribe_user_num_last_month = DB::table('order_day_stats')->whereIn('distribution_channel_id', $channels)->whereBetween('date', [$last_month_begin, $last_month_end])->sum('subscribe_user_num');
                //按站点的
            } else {
                $distribution_channel_id = $item->distribution_channel_id;
                $channel = Channel::getIdById($distribution_channel_id);
                $item->created_at = $channel->created_at;
                $item->charge_amount_yesterday = DB::table('order_day_stats')->where('distribution_channel_id', $distribution_channel_id)->where('date', $date)->sum('total_recharge_amount');
                $subscribe_amount_last_month = DB::table('order_day_stats')->where('distribution_channel_id', $distribution_channel_id)->whereBetween('date', [$last_month_begin, $last_month_end])->sum('subscribe_amount');
                $subscribe_user_num_last_month = DB::table('order_day_stats')->where('distribution_channel_id', $distribution_channel_id)->whereBetween('date', [$last_month_begin, $last_month_end])->sum('subscribe_user_num');
                $item->cost_total = SendOrder::getDuringCostByChannelId($distribution_channel_id, $date . ' 23:59:59');
            }
            $item->start_day_count = ceil((time() - strtotime($item->created_at)) / 86400);
            $item->arup_new_fans_last_month = (0 == $subscribe_user_num_last_month) ? 0 : round((float)$subscribe_amount_last_month / $subscribe_user_num_last_month, 2);
            $item->charge_cost_percent_total = Util:: getPersentAmount($item->charge_amount_total, $item->cost_total);
            $item->charge_cost_percent_yesterday = Util:: getPersentAmount($item->charge_amount_yesterday, $item->cost_total);

            $data[] = array(
                ($type == 'account') ? $item->phone : $item->distribution_channel_id,
                ($type == 'account') ? $item->channel_user_nickname : $item->site_nick_name,
                $item->charge_amount_total,
                $item->cost_total,
                $item->charge_cost_percent_total,
                $item->charge_amount_yesterday,
                $item->charge_cost_percent_yesterday,
                $item->created_at->toDateTimeString(),
                $item->start_day_count,
                $item->arup_new_fans_last_month,
            );
        }
        saveExcelData($header, $data, '', '', '充值数据');
    }
}
